<?php

/**
 * Current conditions entity
 *
 * @copyright  Copyright (c) 2011 Celebrio Software
 * @package    WeatherApi
 */

namespace Celebrio\WeatherApi\WorldWeatherOnline;

use Celebrio\Core\DummyInit;
use Celebrio\WeatherApi\WorldWeatherOnline\WorldWeatherOnline;
use Celebrio\ShadowReader;
use Celebrio\Core\ServiceProvider;
use Celebrio\Modules;
use \dibi;
use \DibiDriverException;

class WWOInit extends DummyInit {

    public function init(array $configuration) {
        parent::init($configuration);
        if(key_exists("private_key", $configuration)) {
            $api_key = $configuration["private_key"];
        } else {
            $sr = new ShadowReader();
            $api_key = $sr->getPassword("wwo");
        }
        
        WorldWeatherOnline::$api_key = $api_key;
        
    }
    
    public function install(array $configuration) {
        parent::install($configuration);
    }
    
    public function uninstall() {
        $name = ServiceProvider::getServiceName(Modules::DB);
        if (($name == "MSSQL") || ($name == "Azure")) {
            try {
                dibi::select("id_user")->from("app_weather_last_use")->execute();
            } catch(DibiDriverException $e) {
                $this->install(array());
            }
        }
        
        parent::uninstall();
    }
}